package com.example.content.adapter.recycle.home

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.*
import com.example.content.activity.PhotoDetailActivity
import com.example.content.adapter.recycle.home.entity.IVItemEntity
import com.example.content.manager.ActivityMgr
import com.example.content.manager.RouterMgr

/**
 * Created by zhudongao
 * Desc
 * Date 2/15/21
 */
class HomeRecyclerAdapter() : BaseMultiItemQuickAdapter<IVItemEntity, BaseViewHolder>(){

    var resourceList = ArrayList<String>()

    init {
        addItemType(GRID_TYPE, R.layout.grid_image_layout)
    }
    override fun convert(holder: BaseViewHolder, item: IVItemEntity) {
        when (holder.itemViewType) {
            GRID_TYPE -> {
                val position = holder.layoutPosition
                if (position in 0..5) {
                    val image = holder.getView<ImageView>(R.id.image)
                    Glide.with(image.context).load(item.coverUrl).into(image)
                    image.setOnClickListener {
                        val bundle = Bundle()
                        bundle.putInt(PRODUCT_ID, item.productId)
                        bundle.putStringArrayList(RESOURCE_LIST, resourceList)
                        bundle.putInt(RESOURCE_POI, position)
                        ActivityMgr.startActivity(PhotoDetailActivity::class.java, bundle)
                    }

                    if (data.size > 6 && position == 5) {
                        val cover = holder.getView<FrameLayout>(R.id.cover_layer)
                        cover.setOnClickListener {
                            val bundle = Bundle()
                            bundle.putInt(PRODUCT_ID, item.productId)
                            bundle.putStringArrayList(RESOURCE_LIST, resourceList)
                            bundle.putInt(RESOURCE_POI, position)
                            ActivityMgr.startActivity(PhotoDetailActivity::class.java, bundle)
                        }
                        cover.visibility = View.VISIBLE
                        val text = holder.getView<TextView>(R.id.img_count)
                        val num = data.size - 6
                        if (num > 9) {
                            text.text = "P$num"
                        } else {
                            text.text = "P0$num"
                        }
                    }
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return if (data.size > 6) 6 else super.getItemCount()
    }

}